home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 January / january_2001.iso / intercd / root / ^4Developers / VB / visbasdb / VBDB / VBDB Code / VB6 Code / Class 6 / Example6-7DE.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-09-17  |  8.4 KB  |  300 lines

  1. VERSION 5.00
  2. Begin VB.Form frmAuthors 
  3.    BorderStyle     =   1  'Fixed Single
  4.    Caption         =   "Authors"
  5.    ClientHeight    =   2625
  6.    ClientLeft      =   45
  7.    ClientTop       =   330
  8.    ClientWidth     =   4680
  9.    LinkTopic       =   "Form1"
  10.    MaxButton       =   0   'False
  11.    MinButton       =   0   'False
  12.    ScaleHeight     =   2625
  13.    ScaleWidth      =   4680
  14.    StartUpPosition =   3  'Windows Default
  15.    Begin VB.CommandButton cmdDone 
  16.       Caption         =   "Do&ne"
  17.       Height          =   375
  18.       Left            =   3240
  19.       TabIndex        =   13
  20.       TabStop         =   0   'False
  21.       Top             =   2160
  22.       Width           =   1215
  23.    End
  24.    Begin VB.CommandButton cmdDelete 
  25.       Caption         =   "&Delete"
  26.       Height          =   375
  27.       Left            =   1680
  28.       TabIndex        =   12
  29.       TabStop         =   0   'False
  30.       Top             =   2160
  31.       Width           =   1215
  32.    End
  33.    Begin VB.CommandButton cmdEdit 
  34.       Caption         =   "&Edit"
  35.       Height          =   375
  36.       Left            =   120
  37.       TabIndex        =   11
  38.       TabStop         =   0   'False
  39.       Top             =   2160
  40.       Width           =   1215
  41.    End
  42.    Begin VB.CommandButton cmdCancel 
  43.       Caption         =   "&Cancel"
  44.       Height          =   375
  45.       Left            =   3240
  46.       TabIndex        =   10
  47.       TabStop         =   0   'False
  48.       Top             =   1680
  49.       Width           =   1215
  50.    End
  51.    Begin VB.CommandButton cmdSave 
  52.       Caption         =   "&Save"
  53.       Height          =   375
  54.       Left            =   1680
  55.       TabIndex        =   9
  56.       TabStop         =   0   'False
  57.       Top             =   1680
  58.       Width           =   1215
  59.    End
  60.    Begin VB.CommandButton cmdAddNew 
  61.       Caption         =   "&Add New"
  62.       Height          =   375
  63.       Left            =   120
  64.       TabIndex        =   8
  65.       TabStop         =   0   'False
  66.       Top             =   1680
  67.       Width           =   1215
  68.    End
  69.    Begin VB.CommandButton cmdNext 
  70.       Caption         =   "Next =>"
  71.       Height          =   255
  72.       Left            =   2520
  73.       TabIndex        =   7
  74.       TabStop         =   0   'False
  75.       Top             =   1320
  76.       Width           =   1215
  77.    End
  78.    Begin VB.CommandButton cmdPrevious 
  79.       Caption         =   "<= Previous"
  80.       Height          =   255
  81.       Left            =   1080
  82.       TabIndex        =   6
  83.       TabStop         =   0   'False
  84.       Top             =   1320
  85.       Width           =   1215
  86.    End
  87.    Begin VB.TextBox txtYearBorn 
  88.       DataField       =   "Year Born"
  89.       DataMember      =   "comAuthors"
  90.       DataSource      =   "denBooks"
  91.       Height          =   285
  92.       Left            =   1200
  93.       Locked          =   -1  'True
  94.       MaxLength       =   4
  95.       TabIndex        =   2
  96.       Text            =   "Text"
  97.       Top             =   840
  98.       Width           =   1215
  99.    End
  100.    Begin VB.TextBox txtAuthor 
  101.       DataField       =   "Author"
  102.       DataMember      =   "comAuthors"
  103.       DataSource      =   "denBooks"
  104.       Height          =   285
  105.       Left            =   1200
  106.       Locked          =   -1  'True
  107.       TabIndex        =   1
  108.       Text            =   "Text2"
  109.       Top             =   480
  110.       Width           =   3375
  111.    End
  112.    Begin VB.TextBox txtAuthorID 
  113.       DataField       =   "Au_ID"
  114.       DataMember      =   "comAuthors"
  115.       DataSource      =   "denBooks"
  116.       Height          =   285
  117.       Left            =   1200
  118.       Locked          =   -1  'True
  119.       TabIndex        =   3
  120.       TabStop         =   0   'False
  121.       Text            =   "Text1"
  122.       Top             =   120
  123.       Width           =   1215
  124.    End
  125.    Begin VB.Label Label3 
  126.       Caption         =   "Year Born"
  127.       Height          =   255
  128.       Left            =   120
  129.       TabIndex        =   5
  130.       Top             =   840
  131.       Width           =   1215
  132.    End
  133.    Begin VB.Label Label2 
  134.       Caption         =   "Author Name"
  135.       Height          =   255
  136.       Left            =   120
  137.       TabIndex        =   4
  138.       Top             =   480
  139.       Width           =   1215
  140.    End
  141.    Begin VB.Label Label1 
  142.       Caption         =   "Author ID"
  143.       Height          =   255
  144.       Left            =   120
  145.       TabIndex        =   0
  146.       Top             =   120
  147.       Width           =   1215
  148.    End
  149. Attribute VB_Name = "frmAuthors"
  150. Attribute VB_GlobalNameSpace = False
  151. Attribute VB_Creatable = False
  152. Attribute VB_PredeclaredId = True
  153. Attribute VB_Exposed = False
  154. Option Explicit
  155. Private Sub ValidateData(AllOK As Boolean)
  156. Dim Message As String
  157. Dim InputYear As Integer, CurrentYear As Integer
  158. AllOK = True
  159. Message = ""
  160. 'Check for name
  161. If Len(txtAuthor.Text) = 0 Then
  162.   Message = "You must enter an Author Name." + vbCrLf
  163.   txtAuthor.SetFocus
  164.   AllOK = False
  165. End If
  166. 'Check length and range on Year Born
  167. InputYear = Val(txtYearBorn.Text)
  168. CurrentYear = Val(Format(Now, "yyyy"))
  169. If Len(txtYearBorn.Text) <> 0 Then
  170.   If InputYear > CurrentYear Or InputYear < CurrentYear - 150 Then
  171.     Message = Message + "Year Born must be between" & Str(CurrentYear - 150) & " and" & Str(CurrentYear) & "."
  172.     txtYearBorn.SetFocus
  173.     AllOK = False
  174.   End If
  175. End If
  176. If Not (AllOK) Then
  177.   MsgBox Message, vbOKOnly + vbInformation, "Validation Error"
  178. End If
  179. End Sub
  180. Private Sub cmdAddNew_Click()
  181. On Error GoTo HandleErrors
  182. Call SetState("Add")
  183. Exit Sub
  184. HandleErrors:
  185. Select Case MsgBox(Err.Description, vbCritical + vbAbortRetryIgnore, "Error Number" + Str(Err.Number) + " in " + Err.Source)
  186. Case vbAbort
  187.   Exit Sub
  188. Case vbRetry
  189.   Resume
  190. Case vbIgnore
  191.   Resume Next
  192. End Select
  193. End Sub
  194. Private Sub cmdCancel_Click()
  195. Call SetState("View")
  196. End Sub
  197. Private Sub cmdDelete_Click()
  198. Dim Response As Integer
  199. On Error GoTo HandleErrors
  200. Response = MsgBox("Are you sure you want to delete this record?", vbYesNo + vbQuestion + vbDefaultButton2, "Delete")
  201. If Response = vbNo Then
  202.   Exit Sub
  203. End If
  204. Exit Sub
  205. HandleErrors:
  206. Select Case MsgBox(Err.Description, vbCritical + vbAbortRetryIgnore, "Error Number" + Str(Err.Number) + " in " + Err.Source)
  207. Case vbAbort
  208.   Exit Sub
  209. Case vbRetry
  210.   Resume
  211. Case vbIgnore
  212.   Resume Next
  213. End Select
  214. End Sub
  215. Private Sub cmdEdit_Click()
  216. Call SetState("Edit")
  217. End Sub
  218. Private Sub cmdNext_Click()
  219. denBooks.rscomAuthors.MoveNext
  220. If denBooks.rscomAuthors.EOF Then
  221.   Beep
  222.   denBooks.rscomAuthors.MoveLast
  223. End If
  224. End Sub
  225. Private Sub cmdPrevious_Click()
  226. denBooks.rscomAuthors.MovePrevious
  227. If denBooks.rscomAuthors.BOF Then
  228.   Beep
  229.   denBooks.rscomAuthors.MoveFirst
  230. End If
  231. End Sub
  232. Private Sub cmdSave_Click()
  233. Dim Valid As Boolean
  234. Call ValidateData(Valid)
  235. If Not (Valid) Then Exit Sub
  236. On Error GoTo HandleErrors
  237. MsgBox "Record saved.", vbOKOnly + vbInformation, "Save"
  238. Call SetState("View")
  239. Exit Sub
  240. HandleErrors:
  241. Select Case MsgBox(Err.Description, vbCritical + vbAbortRetryIgnore, "Error Number" + Str(Err.Number) + " in " + Err.Source)
  242. Case vbAbort
  243.   Exit Sub
  244. Case vbRetry
  245.   Resume
  246. Case vbIgnore
  247.   Resume Next
  248. End Select
  249. End Sub
  250. Private Sub SetState(AppState As String)
  251. Select Case AppState
  252. Case "View"
  253.   txtAuthorID.BackColor = vbWhite
  254.   txtAuthor.Locked = True
  255.   txtYearBorn.Locked = True
  256.   cmdPrevious.Enabled = True
  257.   cmdNext.Enabled = True
  258.   cmdAddNew.Enabled = True
  259.   cmdSave.Enabled = False
  260.   cmdCancel.Enabled = False
  261.   cmdEdit.Enabled = True
  262.   cmdDelete.Enabled = True
  263.   cmdDone.Enabled = True
  264.   txtAuthor.SetFocus
  265. Case "Add", "Edit"
  266.   txtAuthorID.BackColor = vbRed
  267.   txtAuthor.Locked = False
  268.   txtYearBorn.Locked = False
  269.   cmdPrevious.Enabled = False
  270.   cmdNext.Enabled = False
  271.   cmdAddNew.Enabled = False
  272.   cmdSave.Enabled = True
  273.   cmdCancel.Enabled = True
  274.   cmdEdit.Enabled = False
  275.   cmdDelete.Enabled = False
  276.   cmdDone.Enabled = False
  277.   txtAuthor.SetFocus
  278. End Select
  279. End Sub
  280. Private Sub Form_Activate()
  281. Call SetState("View")
  282. End Sub
  283. Private Sub txtAuthor_KeyPress(KeyAscii As Integer)
  284. If KeyAscii = vbKeyReturn Then
  285.   txtYearBorn.SetFocus
  286.   Exit Sub
  287. End If
  288. End Sub
  289. Private Sub txtYearBorn_KeyPress(KeyAscii As Integer)
  290. If KeyAscii = vbKeyReturn Then
  291.   txtAuthor.SetFocus
  292.   Exit Sub
  293. End If
  294. If (KeyAscii >= Asc("0") And KeyAscii <= Asc("9")) Or KeyAscii = vbKeyBack Then
  295.   Exit Sub
  296.   Beep
  297.   KeyAscii = 0
  298. End If
  299. End Sub
  300.